<template>
  <div>
    <el-row>
      <p class="pinformation" style="margin-top: 7px">未排程列表</p>
      <p class="totalStyle" style="margin-top: 5px">共 {{ WProject.length }} 条数据</p>
      <el-row>
        <el-form-item label="合同计划完成时间：" style="margin-left: 10px">
          {{ requireDate }}
        </el-form-item>
      </el-row>
    </el-row>
    <el-table
      style="margin-bottom: 10px"
      :data="WProject"
      v-loading="topLoading"
      ref="wProjectTableRef"
      height="240px"
      @selection-change="handleSelectionChange"
    >
      <el-table-column
        type="selection"
        width="55"
        align="center"
        :selectable="selectable"
        v-if="isSchedu == 0"
      />
      <el-table-column
        show-overflow-tooltip
        label="检测类型"
        align="center"
        prop="jclbId"
        width="250"
      >
        <template #default="scope">{{
          proxy.getItemByArray(jclbList, "jclbId", scope.row.jclbId, "jclbName")
        }}</template>
      </el-table-column>
      <el-table-column
        show-overflow-tooltip
        label="点位名称"
        align="center"
        prop="pointName"
      />
      <el-table-column show-overflow-tooltip label="周期类型" align="center" width="80">
        <template #default="scope">
          <dict-tag :options="sys_point_cycle" :value="scope.row.cycleType" />
        </template>
      </el-table-column>
      <el-table-column
        show-overflow-tooltip
        label="周期次数"
        align="center"
        prop="cycleCount"
        width="80"
      />
      <el-table-column
        show-overflow-tooltip
        label="未排程次数"
        align="center"
        prop="cycleRestCount"
        width="100"
      />
      <el-table-column
        show-overflow-tooltip
        label="检测项目数"
        align="center"
        prop="jcxmCount"
        width="100"
      />
      <el-table-column
        show-overflow-tooltip
        label="分包项目数"
        align="center"
        prop="jcxmPCount"
        width="100"
      />
    </el-table>
    <el-row>
        <el-form-item label="计划开始时间：" v-show="isSchedu == 0">
          <el-date-picker
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择"
            :clearable="false"
            v-model="planDate"
            @change="handlePlanDate()"
          />
        </el-form-item>
        <el-form-item label="当日已有任务：" style="margin-left:10px;">{{ projectCount }} 个</el-form-item>
        <el-form-item style="margin-left:10px;">
        <el-button  link type="success" icon="View" 
          @click="handleJcnr">检测内容</el-button>
        </el-form-item>
        <el-form-item style="margin-left:10px;">
        <el-button type="success" @click="mergeProject" :disabled="isProgect" v-show="isSchedu == 0"
          >单次排程</el-button
        >
      </el-form-item>
      <el-form-item style="margin-left:10px;">
        <el-button type="success" @click="mergeCycleProject" :disabled="isProgect" v-show="isSchedu == 0"
          >周期批量排程</el-button
        >
      </el-form-item>
    </el-row>
    <el-row>
      <el-row style="padding-top: 6px">
        <p class="pinformation">已排程列表</p>
        <p class="totalStyle">共 {{ total }} 条数据</p>
      </el-row>
      <el-row style="padding-bottom: 5px">
        <el-form-item label="未派发：" style="margin-left: 10px"
          >{{ undeliveredNum }} 条</el-form-item
        >
        <el-form-item label="已派发：" style="margin-left: 10px"
          >{{ distributionNum }}条</el-form-item
        >
        <el-button
          type="success"
          @click="handleDelete"
          :disabled="multiple"
          style="margin-left: 10px"
          v-show="isSchedu == 0"
          >批量删除</el-button
        >
        <!-- <el-button
          type="success"
          @click="handeleDistribution"
          :disabled="multiple"
          v-show="isSchedu == 0"
          >批量派发</el-button
        > -->
        <el-checkbox
          style="margin-left: 10px"
          v-model="isAuto"
          label="自动派发"
          @change="isAutoSendChange"
          :disabled="isAutoProject"
          v-show="isSchedu == 0"
        />
      </el-row>
    </el-row>
    <el-table
      :data="YProject"
      ref="yProjectTableRef"
      @selection-change="handleYSelectionChange"
      v-loading="botLoading"
      :row-style="rowProjectStyle"
      border
    >
      <el-table-column type="selection" width="55" align="center" v-if="isSchedu == 0" :selectable="projectSelectable"/>
      <el-table-column
        show-overflow-tooltip
        label="方案编号"
        align="center"
        prop="projectCode"
        width="120"
      />
      <el-table-column
        show-overflow-tooltip
        label="方案名称"
        align="center"
        prop="projectName"
      />
      <el-table-column show-overflow-tooltip label="紧急类型" align="center" width="100">
        <template #default="scope">
          <dict-tag :options="sys_degree" :value="scope.row.degree" />
        </template>
      </el-table-column>
      <el-table-column
        show-overflow-tooltip
        label="计划开始时间"
        align="center"
        prop="planDate"
        width="140"
      />
      <el-table-column
        show-overflow-tooltip
        label="星期"
        align="center"
        prop="weekName"
        width="120"
      />
      <el-table-column
        show-overflow-tooltip
        label="包含周期"
        align="center"
        prop="cycleDesc"
        width="90"
      >
        <template #default="scope">
          {{
            proxy.getItemByArrays(sys_point_cycle, "value", scope.row.cycleDesc, "label")
          }}
        </template>
      </el-table-column>
      <el-table-column
        show-overflow-tooltip
        label="状态"
        align="center"
        prop="isSend"
        width="90"
      >
        <template #default="scope">{{
          scope.row.status == 3
          ? "已撤消" : 
          scope.row.isSend == 2
            ? "已派发"
            : scope.row.isSend == 1
            ? "已生成方案,待派发"
            : "未派发"
        }}</template>
      </el-table-column>
      <el-table-column show-overflow-tooltip label="操作" align="center" width="230">
        <template #default="scope">
          <!-- <el-button  link type="success" 
          @click="handleProjectJcnr(scope.row.projectId)">检测内容</el-button> -->
          <el-button
            v-if="scope.row.status != 3 && scope.row.isSend == 0 && isSchedu == 0"
            link
            type="success"
            @click="handleEdit(scope.row.projectId)"
            >编辑</el-button
          >
          <el-button
            v-if="scope.row.status != 3 && scope.row.isSend == 0 && isSchedu == 0"
            link
            type="success"
            @click="handleDelete(scope.row)"
            >删除</el-button
          >
          <el-button
            v-if="scope.row.status != 3 && scope.row.isSend != 2 && isSchedu == 0"
            link
            type="success"
            @click="handeleDistribution(scope.row)"
            >派发</el-button
          >
          <el-button
            v-if="scope.row.status != 3 && scope.row.isSend == 2"
            link
            type="success"
            @click="handeleCancelProject(scope.row)"
            >撤回</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="pageNum"
      v-model:limit="pageSize"
      @pagination="getList"
    />
    <el-dialog
      :class="'el-dialog-theme3'"
      append-to-body
      draggable
      :close-on-click-modal="false"
      v-model="open"
      :title="title"
      width="1200px"
    >
      <el-form label-width="100px" :model="form" :rules="rules" ref="projectRef">
        <el-row>
          <el-col :span="7">
            <el-form-item label="方案编号：" prop="projectCode" label-width="110px"
              ><el-input
                v-model="form.projectCode"
                placeholder="请输入方案编号"
                maxlength="100"
              ></el-input
            ></el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="方案名称：" label-width="110px" prop="projectName">
              <el-input
                v-model="form.projectName"
                placeholder="请输入方案名称"
                maxlength="100"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="5">
            <el-form-item label="紧急程度：" prop="degree">
              <el-select
                v-model="form.degree"
                placeholder="请选择紧急类型"
                clearable
                filterable
              >
                <el-option
                  v-for="dict in sys_degree"
                  :key="dict.value"
                  :label="dict.label"
                  :value="parseInt(dict.value)"
                />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="7">
            <el-form-item label="计划开始时间：" prop="planDate" label-width="110px">
              <el-date-picker
                type="date"
                value-format="YYYY-MM-DD"
                placeholder="请选择"
                v-model="form.planDate"
                @change="dialogPlanDate"
                style="width: 100%"
                :clearable="false"
              />
            </el-form-item>
          </el-col>
          <el-col :span="10">
            <el-form-item label="计划完成时间：" label-width="110px">
              {{ requireDate }}
            </el-form-item>
          </el-col>
          <el-col :span="7">
            <el-form-item label="方案资质：" prop="zj">
              <el-radio-group v-model="form.zj">
                <el-radio label="G">有资质</el-radio>
                <el-radio label="P">无资质</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <p class="pinformation">未排程列表</p>&nbsp;&nbsp;
          共 {{ dialogWProject.length }} 条数据
        </el-row>
        <el-table
          :data="dialogWProject"
          @selection-change="handleIncreaseSelect"
          height="220"
        >
          <el-table-column
            type="selection"
            width="55"
            align="center"
            :selectable="selectable"
          />
          <el-table-column
            show-overflow-tooltip
            label="检测类型"
            align="center"
            prop="jclbId"
            width="130"
          >
            <template #default="scope">{{
              proxy.getItemByArray(jclbList, "jclbId", scope.row.jclbId, "jclbName")
            }}</template>
          </el-table-column>
          <el-table-column
            show-overflow-tooltip
            label="点位名称"
            align="center"
            prop="pointName"
          />
          <el-table-column
            show-overflow-tooltip
            label="周期类型"
            align="center"
            width="80"
          >
            <template #default="scope">
              <dict-tag :options="sys_point_cycle" :value="scope.row.cycleType" />
            </template>
          </el-table-column>
          <el-table-column
            show-overflow-tooltip
            label="周期次数"
            align="center"
            prop="cycleCount"
            width="80"
          />
          <el-table-column
            show-overflow-tooltip
            label="未排程次数"
            align="center"
            prop="cycleRestCount"
            width="100"
          />
          <el-table-column
            show-overflow-tooltip
            label="检测项目数"
            align="center"
            prop="jcxmCount"
            width="100"
          />
          <el-table-column
            show-overflow-tooltip
            label="分包项目数"
            align="center"
            prop="jcxmPCount"
            width="100"
          />
        </el-table>
        <el-button type="success" style="margin: 10px 0" @click="handeleIncrease"
          >添加</el-button
        >
        <el-row>
            <p class="pinformation">本方案列表</p>&nbsp;&nbsp;
           共 {{ form.projectPcList.length }} 条数据 &nbsp;
            <el-button
              link
              icon="Delete"
              class="deleteBtnColor"
              @click="batchDelete"
              :disabled="isDisable"
              >批量删除</el-button
            >
        </el-row>
        <el-table
          style="margin-bottom: 10px"
          :data="form.projectPcList"
          @selection-change="selectProjectPointList"
          height="220"
        >
          <el-table-column type="selection" width="55" align="center" />
          <el-table-column
            show-overflow-tooltip
            label="检测类型"
            align="center"
            prop="taskPoint.jclbId"
            width="130"
          >
            <template #default="scope">{{
              proxy.getItemByArray(
                jclbList,
                "jclbId",
                scope.row.taskPoint.jclbId,
                "jclbName"
              )
            }}</template>
          </el-table-column>
          <el-table-column
            show-overflow-tooltip
            label="点位名称"
            align="center"
            prop="taskPoint.pointName"
          />
          <el-table-column
            show-overflow-tooltip
            label="周期类型"
            align="center"
            width="80"
          >
            <template #default="scope">
              <dict-tag
                :options="sys_point_cycle"
                :value="scope.row.taskPoint.cycleType"
              />
            </template>
          </el-table-column>
          <el-table-column
            show-overflow-tooltip
            label="周期次数"
            align="center"
            prop="taskPoint.cycleCount"
            width="80"
          />
          <el-table-column
            show-overflow-tooltip
            label="检测项目数"
            align="center"
            prop="taskPoint.jcxmCount"
            width="100"
          />
          <el-table-column
            show-overflow-tooltip
            label="分包项目数"
            align="center"
            prop="taskPoint.jcxmPCount"
            width="100"
          />
        </el-table>
        <el-row>
          <el-col :span="14">
            <el-form-item label="当日已有任务："
              >{{ dialogProjectCount }} 个</el-form-item
            >
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="success" @click="submit">保 存</el-button>
          <el-button type="danger" @click="cancal">取 消</el-button>
        </div>
      </template>
    </el-dialog>
    <jcnrDialog ref="jcnrDialogRef"  v-if="showJcnr"/>
    <el-dialog
    :class="'el-dialog-theme3'"
    v-model="showProjectJcnr"
    fullscreen
    class="ptDialog roll-dialog pub_dialog"
    width="90%"
    title="检测内容"
    top="1vh"
    draggable
    :close-on-click-modal="false"
    align-center
  >
    <projectJcnrDialog ref="projectJcnrDialogRef"/>
    </el-dialog>
  </div>
</template>

<script setup>
import { getNextDays } from "@/api/common";
import {
  getWProject,
  getYProject,
  getContract,
  getProjectCount,
  addProject,
  addBatchProject,
  sendProject,
  autoSendProject,
  pcListProject,
  getSendCount,
  delProject,
  addEiditProject,
  cancelProject,
} from "@/api/contract/project";
import useRouteView from "@/store/modules/routeView";
import jcnrDialog from "@/views/contract/point/jcnr.vue"
import projectJcnrDialog from "@/views/kshtj/taskProgressDetails/components/jcnr.vue";

const { proxy } = getCurrentInstance();
const router = useRouter();
const route = useRoute();
const routeViewStore = useRouteView();
const { sys_point_cycle, sys_degree } = proxy.useDict("sys_point_cycle", "sys_degree");
const { jclbList } = proxy.useJclb();
const open = ref(false);
const topLoading = ref(false);
const botLoading = ref(false);
const title = ref("编辑排程方案");
const isAutoSend = ref();
const isAuto = ref(1);
const total = ref(0);
const planDate = ref("");
const WProject = ref([]);
const dialogWProject = ref([]);
const YProject = ref([]);
const requireDate = ref();
const projectPcList = ref([]);
const increaseProject = ref([]);
const projectCount = ref(0);
const dialogProjectCount = ref(0);
const pageNum = ref(1);
const pageSize = ref(10);
const multiple = ref(true);
const isAutoProject = ref();
//当前合同是否全部完成排程派发 1：是 0：否。
const isSchedu = ref(1);
//点击复选框选择的检测点位id
const pointIds = ref([]);
const projectIds = ref([]);
const undeliveredNum = ref("0");
const distributionNum = ref("0");
const showJcnr = ref(false);
const showProjectJcnr = ref(false);
const zjList = ref([
  { value: "0", label: "有" },
  { value: "1", label: "无" },
]);
// 点击复选框拿到当前选中行
const wSelection = ref([]);
const props = defineProps({
  contractId: {
    type: String, //参数类型
  },
  rowClickNum: {
    type: Number,
  },
});
const data = reactive({
  form: {
    projectPcList: [],
  },
  rules: {
    projectName: [{ required: true, message: "方案名称不能为空", trigger: "blur" }],
    planDate: [{ required: true, message: "计划开始时间不能为空", trigger: "blur" }],
  },
});

const { form, rules } = toRefs(data);

const isProgect = ref(true);
/** 选择条数  */
function handleSelectionChange(selection) {
  pointIds.value = selection.map((item) => item.pointId);
  wSelection.value = selection;
  if (selection.length == 0) isProgect.value = true;
  if (selection.length != 0) isProgect.value = false;
}

/** 选择条数  */
function handleYSelectionChange(selection) {
  projectIds.value = selection.map((item) => item.projectId);
  for (let i = 0; i < selection.length; i++) {
    if (selection[i].isSend == 2) multiple.value = true;
    if (selection[i].isSend == 0 || selection[i].isSend == 1) multiple.value = false;
    if (multiple.value) break;
  }
}

//单次合并方案
function mergeProject() {
  if (pointIds.value.length == 0) {
    proxy.$modal.msgError(`请选择要排程的检测点位！`);
  } else {
    let nextDay = planDate.value.split("-");
    let temp = {
      pointIds: pointIds.value,
      contractId: props.contractId,
      planDate: nextDay[0] + "年" + nextDay[1] + "月" + nextDay[2] + "日",
    };
    addProject(temp).then(() => {
      getLists();
      handlePlanDate();
    });
  }
}

watch(
  () => route,
  (newVal) => {
    if (newVal.path == "/flowable/task/record/index") {
      const { path, name, meta } = newVal;
      routeViewStore.setRouteView1({ path, name, meta });
    }
  }
);

function routePush(row) {
  if (routeViewStore.routeView1) {
    proxy.$tab.closePage(routeViewStore.routeView1);
  }
  router.push({
    path: "/flowable/task/record/index",
    query: {
      procInsId: row.procInsId,
      executionId: row.executionId,
      deployId: row.deployId,
      taskId: row.taskId,
      finished: true,
      path: "/contract/pcSend",
    },
  });
}

// 派发按钮和批量派发
function handeleDistribution(row) {
  if (row.projectId) projectIds.value = [row.projectId];
  if (projectIds.value.length == 0) {
    proxy.$modal.msgError(`请选择要派发的检测点位！`);
  } else {
    if (row.isSend == 1) {
      proxy.$modal.loading("正在派发，请稍候...");
      sendProject(projectIds.value)
        .then((flow) => {
          proxy.$modal.msgSuccess(`派发成功`);
          proxy.$modal.closeLoading();
          routePush(flow.data);
          getList();
        })
        .catch(() => {
          proxy.$modal.closeLoading();
        });
      return;
    }
    proxy.$modal.loading("正在生成方案信息，请稍候...");
    let startTime = new Date().getTime();
    sendProject(projectIds.value)
      .then((res) => {
        console.log("msg:" + res.msg);
        if (res.msg == "生成方案信息成功！") {
          row.isSend = 1;
          proxy.$modal.closeLoading();
          proxy.$modal.loading("正在派发中，请稍候...");
          let diffTime = new Date().getTime() - startTime;
          if (diffTime < 1000) {
            setTimeout(() => {
              sendProject(projectIds.value)
                .then((flow) => {
                  proxy.$modal.msgSuccess(`派发成功`);
                  proxy.$modal.closeLoading();
                  routePush(flow.data);
                  getList();
                })
                .catch(() => {
                  proxy.$modal.closeLoading();
                });
            }, 1000 - diffTime);
            return;
          }
          sendProject(projectIds.value)
            .then((flow) => {
              proxy.$modal.msgSuccess(`派发成功`);
              proxy.$modal.closeLoading();
              routePush(flow.data);
              getList();
            })
            .catch(() => {
              proxy.$modal.closeLoading();
            });
        } else {
          proxy.$modal.msgError(res.msg);
        }
      })
      .catch(() => {
        proxy.$modal.closeLoading();
      });
  }
}

function handeleCancelProject(row) {
  let params = {};
  params.projectIds = [row.projectId];
  proxy.$modal
    .confirm("撤回方案会删除方案中所有的数据，确定要撤回派发的方案吗？")
    .then(function () {
      proxy.$modal.loading("正在撤回方案，请稍候...");
      cancelProject(params)
        .then((res) => {
          if (res.code == 200) {
            getList();
            proxy.$modal.closeLoading();
            proxy.$modal.msgSuccess("撤回成功，请重新排程！");
          }
        })
        .catch(() => {
          proxy.$modal.closeLoading();
          proxy.$modal.msgError("撤回方案失败，请联系管理员");
        });
    });
}
// 删除按钮
function handleDelete(row) {
  if (row.projectId) projectIds.value = [row.projectId];
  proxy.$modal
    .confirm("确认删除该条数据吗？")
    .then(function () {
      return delProject(projectIds.value);
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("删除成功");
    })
    .catch(() => {});
}

//按周期合并方案
function mergeCycleProject() {
  if (pointIds.value.length == 0) {
    proxy.$modal.msgError(`请选择要排程的检测点位！`);
  } else {
    let isSame = true;
    for (let index = 0; index < wSelection.value.length - 1; index++) {
      let item = wSelection.value[index];
      isSame =
        item.cycleType == wSelection.value[index + 1].cycleType &&
        item.cycleRestCount == wSelection.value[index + 1].cycleRestCount;
      if (!isSame) break;
    }
    if (isSame) {
      let nextDay = planDate.value.split("-");
      let temp = {
        pointIds: pointIds.value,
        contractId: props.contractId,
        planDate: nextDay[0] + "年" + nextDay[1] + "月" + nextDay[2] + "日",
      };
      proxy.$modal.loading("正在批量排程，请稍候...");
      addBatchProject(temp)
        .then((res) => {
          getLists();
          proxy.$modal.msgSuccess(`周期批量排程成功！`);
          proxy.$modal.closeLoading();
          handlePlanDate();
        })
        .catch((res) => {
          proxy.$modal.closeLoading();
        });
    } else {
      proxy.$modal.msgError(`批量周期排程失败。所选点位周期类型或次数不同，请检查！`);
    }
  }
}
//计划开始时间改变后调用接口获取当天的任务数量
function handlePlanDate() {
  projectCount.value = 0;
  getProjectCount({ planDate: planDate.value }).then((res) => {
    projectCount.value = res.data;
  });
}
function dialogPlanDate() {
  dialogProjectCount.value = 0;
  getProjectCount({ planDate: form.value.planDate }).then((res) => {
    dialogProjectCount.value = res.data;
  });
}

watch(
  () => props.rowClickNum,
  (val) => {
    if (props.contractId) getList();
  },
  { deep: true, immediate: true }
);

watch(
  () => YProject,
  (val) => {
    if (YProject.value.length == 0) {
      isAutoProject.value = true;
    } else {
      for (let i = 0; i < YProject.value.length; i++) {
        if (YProject.value[i].isSend == 0 || YProject.value[i].isSend == 1)
          isAutoProject.value = false;
        if (YProject.value[i].isSend == 2) isAutoProject.value = true;
        if (!isAutoProject.value) break;
      }
    }
  },
  { deep: true, immediate: true }
);

function getSendCountList() {
  getSendCount(props.contractId).then((res) => {
    undeliveredNum.value = res.data[0];
    distributionNum.value = res.data[1];
  });
}

function getLists() {
  topLoading.value = true;
  botLoading.value = true;
  getContract(props.contractId).then((res) => {
    isSchedu.value = res.data.isSchedu;
    requireDate.value = res.data.requireDate;
    isAutoSend.value = res.data.isAutoSend;
    isAutoSend.value ? (isAuto.value = true) : (isAuto.value = false);
  });
  getWProject({ primaryId: props.contractId }).then((res) => {
    WProject.value = res.data;
    topLoading.value = false;
  });
  getYProject({
    contractId: props.contractId,
    pageNum: pageNum.value,
    pageSize: pageSize.value,
  }).then((res) => {
    YProject.value = res.rows;
    YProject.value.forEach((item, index) => {
      YProject.value[index].cycleDesc = item.cycleDesc.split(",");
    });
    total.value = res.total;
    botLoading.value = false;
  });
  getSendCountList();
}

function getList() {
  getLists();
  getNextDays().then((res) => {
    planDate.value = res.data;
    handlePlanDate();
  });
}

function selectable(row) {
  if (row.cycleRestCount) {
    return true;
  } else {
    return false;
  }
}

function handleEdit(projectId) {
  open.value = true;
  form.value.projectId = projectId;
  pcListProject(projectId).then((res) => {
    dialogWProject.value = JSON.parse(JSON.stringify(WProject.value));
    form.value = res.data;
    dialogPlanDate();
    form.value.projectPcList.forEach((row) => {
      dialogWProject.value = dialogWProject.value.filter((item) => {
        return row.taskPoint.pointId != item.pointId;
      });
    });
  });
}

watch(
  () => total.value,
  () => {
    if (total.value) {
      heightParams.value = [{ tableName: "yProject", bottomHeight: 74 }];
    } else {
      heightParams.value = [{ tableName: "yProject", bottomHeight: 21 }];
    }
    getTableHeight();
  }
);

const heightParams = ref([{ tableName: "yProject", bottomHeight: 21 }]);

function getTableHeight() {
  proxy.adaptiveHeight(proxy, heightParams.value);
}

onMounted(() => {
  nextTick(function () {
    getTableHeight();
    window.addEventListener("resize", getTableHeight);
  });
});

onUpdated(() => {
  nextTick(function () {
    getTableHeight();
  });
});

onBeforeUnmount(() => {
  window.removeEventListener("resize", getTableHeight);
});

function isAutoSendChange() {
  isAuto.value ? (isAutoSend.value = 1) : (isAutoSend.value = 0);
  autoSendProject({
    contractId: props.contractId,
    isAutoSend: isAutoSend.value,
  }).then((res) => {
    if (isAutoSend.value) {
      proxy.$modal.msgSuccess("自动派发成功");
    } else {
      proxy.$modal.msgSuccess("取消自动派发成功");
    }
  });
}

function batchDelete() {
  projectPcList.value.forEach((item) => {
    form.value.projectPcList = form.value.projectPcList.filter((row) => {
      return item.taskPoint.pointId != row.taskPoint.pointId;
    });
    let temp = item.taskPoint;
    temp.cycleRestCount = item.taskPoint.cycleRestCount + 1;
    temp.pointId = item.pointId;
    dialogWProject.value.push(temp);
  });
  projectPcList.value = [];
}

const isDisable = ref(true);
function selectProjectPointList(select) {
  projectPcList.value = select;
  if (select.length == 0) isDisable.value = true;
  if (select.length != 0) isDisable.value = false;
}

function handleIncreaseSelect(select) {
  increaseProject.value = select;
}
function handeleIncrease() {
  increaseProject.value.forEach((item) => {
    dialogWProject.value = dialogWProject.value.filter((row) => {
      return item.pointId != row.pointId;
    });
    let temp = {};
    temp.pointId = item.pointId;
    temp.taskPoint = item;
    temp.taskPoint.cycleRestCount = item.cycleRestCount - 1;
    form.value.projectPcList.push(temp);
  });
}

function submitList() {
  proxy.$refs["projectRef"].validate((valid) => {
    if (valid) {
      let nextDay = form.value.planDate.split("-");
      let temp = {
        projectCode: form.value.projectCode,
        projectName: form.value.projectName,
        projectId: form.value.projectId,
        planDate: nextDay[0] + "年" + nextDay[1] + "月" + nextDay[2] + "日",
        degree: form.value.degree,
        pointIds: form.value.projectPcList.map((item) => item.pointId),
        zj: form.value.zj,
      };
      addEiditProject(temp).then((res) => {
        open.value = false;
        proxy.$modal.msgSuccess(res.msg);
        getList();
      });
    }
  });
}

function submit() {
  if (form.value.projectPcList.length == 0) {
    proxy.$modal
      .confirm("本方案列表的检测点位为空，保存后会删除本方案，您确认要继续吗？")
      .then(function () {
        submitList();
      })
      .catch(() => {});
  } else {
    submitList();
  }
}

function cancal() {
  open.value = false;
}

function handleJcnr(){
  showJcnr.value = true;
  nextTick(() => {
    proxy.$refs["jcnrDialogRef"].getForm(props.contractId);
    proxy.$refs["jcnrDialogRef"].isShow = true
  });
}

function handleProjectJcnr(projectId){
  debugger;
  showProjectJcnr.value = true;
  nextTick(() => { 
    proxy.$refs["projectJcnrDialogRef"].getForm(projectId);
  });
}

function rowProjectStyle({ row }) {
  if (row.status == 3) {
    return { background: "#a8abb2" };
  }
}

function projectSelectable(row){
  return row.status != 3
}

</script>

<style></style>
